#include "../common/icsearch.hpp"
#include <gflags/gflags.h>

DEFINE_bool(run_mode, false, "程序的运行模式, false-调试, true-发布");
DEFINE_string(log_file, "", "发布模式下用于指定日志的输出文件");
DEFINE_int32(log_level, 0, "发布模式下，用于指定日志输出等级");

int main(int argc, char* argv[])
{
    google::ParseCommandLineFlags(&argc, &argv, true);
    init_logger(FLAGS_run_mode, FLAGS_log_file, FLAGS_log_level);
    
    std::vector<std::string> host_list = {"http://127.0.0.1:9200/"};
    auto client = std::make_shared<elasticlient::Client>(host_list);

    bool ret = ESIndex(client, "test_user", "_doc").append("nickname")
    .append("phone", "keyword", "standard", true)
    .create();

    if(ret == false){
        LOG_INFO("索引创建失败！");
        return -1;
    }else{
        LOG_INFO("索引创建成功！");
    }

    //数据新增
    ret = ESInsert(client, "test_user", "_doc")
    .append("nickname", "程序吗喽")
    .append("phone", "155555588888")
    .insert("00000001");

    if(ret == false){
        LOG_ERROR("数据插入失败");
        return -1;
    }else{
        LOG_INFO("数据插入成功！");
    }



    //数据修改
    ret = ESInsert(client, "test_user", "_doc")
    .append("nickname", "程序吗喽")
    .append("phone", "155555577777")
    .insert("00000001");

    if(ret == false){
        LOG_ERROR("数据更新失败");
        return -1;
    }else{
        LOG_INFO("数据更新成功！");
    }



    Json::Value user = ESSearch(client, "test_user", "_doc")
    .append_should_match("phone.keyword", "155555577777")
    .search();

    if(user.empty() || user.isArray() == false){
        LOG_ERROR("结果为空，或者结果不是数组类型!");
        return -1;
    }else{
        LOG_INFO("数据检索成功！");
    }


    int sz = user.size();
    for(int i = 0; i < sz; ++i){
        std::cout << user[i]["nickname"].asString() << std::endl;
    }

    ret = ESRemove(client, "test_user", "_doc").remove("00000001");
    
    if(ret == false){
        LOG_ERROR("删除数据失败");
        return -1;
    }else{
        LOG_INFO("数据删除成功！");
    }



    return 0;
}